package com.jason.quectel0805.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jason.quectel0805.R;
import com.jason.quectel0805.entities.Contact;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author：Jason
 * @date：2022/8/9 15:46
 * @email：1129847330@qq.com
 * @description:
 */
public class WechatContactAdapter extends RecyclerView.Adapter<WechatContactAdapter.WechatContactViewHolder> {
    private List<Contact> list = new ArrayList<>();
    private LayoutInflater inflater;
    private Context context;

    public void setList(List<Contact> list) {
        if (list != null) {
            this.list.addAll(list);
        }
        notifyDataSetChanged();
    }

    public WechatContactAdapter(List<Contact> list, Context context) {
        if (list != null) {
            this.list.addAll(list);
        }
        inflater = LayoutInflater.from(context);
        this.context = context;
    }

    @NonNull
    @Override

    public WechatContactAdapter.WechatContactViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_wechat_contact, null);
        WechatContactAdapter.WechatContactViewHolder viewHolder = new WechatContactAdapter.WechatContactViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull WechatContactAdapter.WechatContactViewHolder holder, int position) {
        Contact contact = list.get(position);
        holder.tvNickname.setText(contact.getNickname());
        Glide.with(context).load(contact.getAvatar()).into(holder.ivAvatar);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class WechatContactViewHolder extends RecyclerView.ViewHolder {
        ImageView ivAvatar;
        TextView tvNickname;

        public WechatContactViewHolder(@NonNull View itemView) {
            super(itemView);
            ivAvatar = itemView.findViewById(R.id.iv_contact_avatar);
            tvNickname = itemView.findViewById(R.id.tv_contact_nickname);
        }
    }
}